package com.XinYun.Library.modules.bookcart.delete.controller;

import com.XinYun.Library.modules.bookcart.delete.entity.Book;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name = "deleteServlet", value = "/deleteServlet")
public class deleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] items = request.getParameterValues("deleteItem");
        HttpSession session = request.getSession();
        ArrayList<Book> cart = (ArrayList<Book>) session.getAttribute("cart");
        if(items != null && cart != null) {
            for(String item : items) {
                String id = String.valueOf(Integer.parseInt(item));
                for(Book book : cart) {
                    if(book.getId() == id) {
                        cart.remove(book);
                        break;
                    }
                }
            }
        }
        session.setAttribute("cart", cart);
        response.sendRedirect(request.getContextPath() + "/cart.jsp");
    }
}




